package com.myk.game.heroscuffle.heroes;

import com.myk.game.heroscuffle.game.Element;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.skills.Skill_ZhiLiao;
import com.myk.game.heroscuffle.utils.RdmPropUtil;

public class Hero_YouLan extends Hero {

    /**
     * 基础数值都是最低的，但是有各种保命手段
     */
    public Hero_YouLan() {
        super("幽兰",
                "回合开始前会根据场上冰和地属性英雄的数量恢复一定hp。",
                Element.WOOD, 1100, 120, 40,
                new Skill_ZhiLiao());
    }

    @Override
    public void onPreAction() {
        if (hp < HP) {
            int meetCount = game.pool.getAllAliveHeroesBy(item -> item.ELE == Element.ICE || item.ELE == Element.EARTH).size();
            if (meetCount > 0) {
                game.ui.printlnMsg(getObviousName() + "自然生长，");
                int regain = meetCount * 20;
                addHpWithPrint(regain);
                game.ui.sleepShortly();
            }
        }
    }

    @Override
    public void onAutoSelectAction() {
        //自己半血了提高治疗别人的概率
        //不救残血的敌人
        //不救满血的陌生人
        Skill_ZhiLiao zhiLiao = (Skill_ZhiLiao) getUsableSkill(Skill_ZhiLiao.class); //获取可用的技能救死扶伤
        if (zhiLiao != null) {
            boolean isHalfHealth = hp < HP / 2; //自己是否低血量
            if (isHalfHealth) { //自己残血，赶紧救别人
                if (RdmPropUtil.bingo(1 / 2d)) { //更大的几率救别人
                    Hero intentPatient = selectPatient();
                    if (intentPatient != null) {
                        zhiLiao.setIntentPatient(intentPatient);
                        useSkill(zhiLiao);
                    }
                    else {
                        super.onAutoSelectAction();
                    }
                }
                else {
                    super.onAutoSelectAction();
                }
            }
            else { //自己不残血，小概率救别人
                if (RdmPropUtil.bingo(1 / 3d)) { //更小的几率救别人
                    Hero intentPatient = selectPatient();
                    if (intentPatient != null) {
                        zhiLiao.setIntentPatient(intentPatient);
                        useSkill(zhiLiao);
                    }
                    else {
                        super.onAutoSelectAction();
                    }
                }
                else {
                    super.onAutoSelectAction();
                }
            }

        }
        else {//无技能只能进行普攻
            super.onAutoSelectAction();
        }
    }

    //用此方法寻找意向的病人。可以避免救没病的人和残血的敌人
    private Hero selectPatient() {
        Hero prePatient1 = game.pool.getAnyoneAliveHero(this); //肯定不为null
        if (checkPatient(prePatient1)){
            return prePatient1;
        }
        Hero prePatient2 = game.pool.getAnyoneAliveHero(this,prePatient1);
        if (checkPatient(prePatient2)){
            return prePatient2;
        }
        Hero prePatient3 = game.pool.getAnyoneAliveHero(this,prePatient1,prePatient2);
        if (checkPatient(prePatient3)){
            return prePatient3;
        }
        return null;
    }

    //判断此候选病人是否符合要求？（不能是残血的敌人和满血的陌生人）
    private boolean checkPatient(Hero prePatient) {
        if (prePatient == null){
            return false;
        }
        if (prePatient == state.getAliveEnemy()) {
            return prePatient.hp > prePatient.HP / 3;
        }
        else {
            return (prePatient.HP - prePatient.hp) >= Skill_ZhiLiao.HEALING_AMOUNT;
        }
    }

}
